package com.cskaoyan.catmall.service.Impl;

import com.cskaoyan.catmall.dao.*;
import com.cskaoyan.catmall.model.bo.WxGoodsListBo;
import com.cskaoyan.catmall.model.pojo.*;
import com.cskaoyan.catmall.model.vo.WxCategoryVo;
import com.cskaoyan.catmall.model.vo.WxGoodsDetailVo;
import com.cskaoyan.catmall.model.vo.WxGoodsVo;
import com.cskaoyan.catmall.model.vo.WxRelatedGoodsVo;
import com.cskaoyan.catmall.service.WxGoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 创建日期: 2022-01-10-19-38
 * author : LL
 * description:
 */
@Service
public class WxGoodsServiceImpl implements WxGoodsService {
    @Autowired
    MarketGoodsMapper goodsMapper;

    @Autowired
    MarketCategoryMapper categoryMapper;

    @Autowired
    MarketBrandMapper brandMapper;

    @Autowired
    MarketCommentMapper commentMapper;

    @Autowired
    MarketIssueMapper issueMapper;

    @Autowired
    MarketGoodsSpecificationMapper goodsSpecificationMapper;

    @Autowired
    MarketGrouponMapper grouponMapper;

    @Autowired
    MarketGoodsProductMapper goodsProductMapper;

    @Autowired
    MarketGoodsAttributeMapper goodsAttributeMapper;

    @Autowired
    MarketCollectMapper collectMapper;

    @Autowired
    MarketFootprintMapper footprintMapper;


    @Override
    public int getGoodsCount() {
        int count = goodsMapper.selectGoodsCount();
        return count;
    }

    @Override
    public WxCategoryVo getCategoryById(Integer id) {
        WxCategoryVo categoryVo = new WxCategoryVo();
        MarketCategory category = categoryMapper.selectByPrimaryKey(id);
        if (category.getPid() == 0) {
            categoryVo.setParentCategory(category);
            MarketCategoryExample categoryExample = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria = categoryExample.createCriteria();
            criteria.andPidEqualTo(category.getId());
            criteria.andDeletedEqualTo(false);
            List<MarketCategory> categoryList = categoryMapper.selectByExample(categoryExample);
            categoryVo.setBrotherCategory(categoryList);
            categoryVo.setCurrentCategory(categoryList.get(0));
        } else {
            categoryVo.setCurrentCategory(category);
            MarketCategory parentCategory = categoryMapper.selectByPrimaryKey(category.getPid());
            MarketCategoryExample categoryExample = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria = categoryExample.createCriteria();
            criteria.andPidEqualTo(category.getPid());
            criteria.andDeletedEqualTo(false);
            List<MarketCategory> categoryList = categoryMapper.selectByExample(categoryExample);
            categoryVo.setBrotherCategory(categoryList);
            categoryVo.setParentCategory(parentCategory);
        }

        return categoryVo;
    }

    @Override
    public WxGoodsVo getGoodsSelective(WxGoodsListBo goodsListBo) {
        List<Integer> categoryIds = new ArrayList<>();
        List<MarketCategory> filterCategoryList = null;


        categoryIds = goodsMapper.selectCategoryIdsSelective(goodsListBo);


        if (categoryIds.size() != 0) {
            MarketCategoryExample categoryExample = new MarketCategoryExample();
            MarketCategoryExample.Criteria criteria = categoryExample.createCriteria();

            MarketCategory category = categoryMapper.selectByPrimaryKey(goodsListBo.getCategoryId());
            criteria.andIdIn(categoryIds);
            criteria.andDeletedEqualTo(false);
            filterCategoryList = categoryMapper.selectByExample(categoryExample);
        }


        List<WxGoodsVo.ListDTO> list = goodsMapper.selectGoodsSelective(goodsListBo);
        WxGoodsVo goodsVo = new WxGoodsVo();
        goodsVo.setList(list);
        goodsVo.setFilterCategoryList(filterCategoryList);
        return goodsVo;
    }

    @Override
    public WxGoodsDetailVo getGoodsDetailByGoodsId(Integer goodsId) {
        MarketGoods goodsInfo = goodsMapper.selectByPrimaryKey(goodsId);
        if (goodsInfo == null) {
            return null;
        }

        MarketCommentExample commentExample = new MarketCommentExample();
        MarketCommentExample.Criteria commentExampleCriteria = commentExample.createCriteria();
        commentExampleCriteria.andValueIdEqualTo(goodsId);
        commentExampleCriteria.andDeletedEqualTo(false);
        PageHelper.startPage(1, 10);
        List<MarketComment> comments = commentMapper.selectByExample(commentExample);
        PageInfo<MarketComment> commentPageInfo = new PageInfo<>(comments);

        MarketBrand brand = brandMapper.selectByPrimaryKey(goodsInfo.getBrandId());

        MarketGoodsAttributeExample goodsAttributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria goodsAttributeExampleCriteria = goodsAttributeExample.createCriteria();
        goodsAttributeExampleCriteria.andGoodsIdEqualTo(goodsId);
        goodsAttributeExampleCriteria.andDeletedEqualTo(false);
        List<MarketGoodsAttribute> attributes = goodsAttributeMapper.selectByExample(goodsAttributeExample);

        MarketIssueExample issueExample = new MarketIssueExample();
        MarketIssueExample.Criteria issueExampleCriteria = issueExample.createCriteria();
        issueExampleCriteria.andDeletedEqualTo(false);
        List<MarketIssue> issues = issueMapper.selectByExample(issueExample);

        MarketGoodsProductExample goodsProductExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria goodsProductExampleCriteria = goodsProductExample.createCriteria();
        goodsProductExampleCriteria.andGoodsIdEqualTo(goodsId);
        goodsProductExampleCriteria.andDeletedEqualTo(false);
        List<MarketGoodsProduct> productList = goodsProductMapper.selectByExample(goodsProductExample);

        MarketGoodsSpecificationExample goodsSpecificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria goodsSpecificationExampleCriteria = goodsSpecificationExample.createCriteria();
        goodsSpecificationExampleCriteria.andGoodsIdEqualTo(goodsId);
        goodsSpecificationExampleCriteria.andDeletedEqualTo(false);
        List<MarketGoodsSpecification> specificationList = goodsSpecificationMapper.selectByExample(goodsSpecificationExample);


        MarketCollectExample collectExample = new MarketCollectExample();
        MarketCollectExample.Criteria collectExampleCriteria = collectExample.createCriteria();
        collectExampleCriteria.andDeletedEqualTo(false);
        collectExampleCriteria.andTypeEqualTo((byte) 0);
        collectExampleCriteria.andValueIdEqualTo(goodsId);
        List<MarketCollect> collectList = collectMapper.selectByExample(collectExample);

        WxGoodsDetailVo goodsDetailVo = new WxGoodsDetailVo();
        goodsDetailVo.setAttribute(attributes);
        goodsDetailVo.setBrand(brand);
        goodsDetailVo.setComment(new WxGoodsDetailVo.CommentDTO(comments, (int) commentPageInfo.getTotal()));
        goodsDetailVo.setInfo(goodsInfo);
        goodsDetailVo.setIssue(issues);
        goodsDetailVo.setProductList(productList);
        List<WxGoodsDetailVo.SpecificationListDTO> specificationListDTOS = new ArrayList<>();
        HashSet<String> names = new HashSet<>();
        if (specificationList != null && specificationList.size() != 0) {
            for (MarketGoodsSpecification goodsSpecification : specificationList) {
                names.add(goodsSpecification.getSpecification());
            }
        }

        HashMap<String, List<MarketGoodsSpecification>> specificationMap = new HashMap<>();
        for (String name : names) {
            List<MarketGoodsSpecification> goodsSpecifications = new ArrayList<>();
            for (int i = 0; i < specificationList.size(); i++) {
                if (name.equals(specificationList.get(i).getSpecification())) {
                    goodsSpecifications.add(specificationList.get(i));
                }
            }
            specificationMap.put(name, goodsSpecifications);

        }
        for (String name : names) {
            for (Map.Entry<String, List<MarketGoodsSpecification>> stringListEntry : specificationMap.entrySet()) {
                if (name.equals(stringListEntry.getKey())) {
                    specificationListDTOS.add(new WxGoodsDetailVo.SpecificationListDTO(name, stringListEntry.getValue()));
                }
            }
        }
        goodsDetailVo.setSpecificationList(specificationListDTOS);
        goodsDetailVo.setUserHasCollect(collectList.size());
        return goodsDetailVo;
    }

    @Override
    public WxGoodsVo getGoodsByBrandId(Integer brandId) {
        List<WxGoodsVo.ListDTO> list = goodsMapper.selectGoodsByBrandId(brandId);

        List<Integer> categoryIds = goodsMapper.selectCategoryIdsByBrandId(brandId);

        MarketCategoryExample categoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria categoryExampleCriteria = categoryExample.createCriteria();
        categoryExampleCriteria.andDeletedEqualTo(false);
        if (categoryIds != null && categoryIds.size() != 0) {
            categoryExampleCriteria.andIdIn(categoryIds);
        }
        List<MarketCategory> filterCategoryList = categoryMapper.selectByExample(categoryExample);

        WxGoodsVo goodsVo = new WxGoodsVo();
        goodsVo.setList(list);
        goodsVo.setFilterCategoryList(filterCategoryList);
        return goodsVo;
    }

    @Override
    public WxRelatedGoodsVo getRelatedGoods(Integer id) {
        int categoryId = goodsMapper.selectCategoryIdById(id);
        PageHelper.startPage(1, 6);
        List<WxGoodsVo.ListDTO> listDTOS = goodsMapper.selectGoodsByCategoryId(categoryId);
        PageInfo<WxGoodsVo.ListDTO> pageInfo = new PageInfo<>(listDTOS);

        WxRelatedGoodsVo relatedGoodsVo = new WxRelatedGoodsVo();
        relatedGoodsVo.setList(listDTOS);
        relatedGoodsVo.setTotal((int) pageInfo.getTotal());
        relatedGoodsVo.setPages(pageInfo.getPages());
        relatedGoodsVo.setLimit(6);
        relatedGoodsVo.setPage(1);
        return relatedGoodsVo;
    }

    @Override
    public void insertFootPrint(MarketUser principal,Integer id) {
        MarketFootprintExample footprintExample = new MarketFootprintExample();
        MarketFootprint footprint = new MarketFootprint();
        footprintExample.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(principal.getId());
        List<MarketFootprint> footprints = footprintMapper.selectByExample(footprintExample);
        for (MarketFootprint footprint1 : footprints) {
            if (footprint1.getGoodsId().equals(id)){
                MarketFootprintExample marketFootprintExample = new MarketFootprintExample();
                marketFootprintExample.createCriteria().andDeletedEqualTo(false).andUserIdEqualTo(principal.getId()).andGoodsIdEqualTo(id);
                footprint.setUpdateTime(new Date());
                footprintMapper.updateByExampleSelective(footprint,marketFootprintExample);
                return;
            }
        }

        footprint.setAddTime(new Date());
        footprint.setUpdateTime(new Date());
        footprint.setGoodsId(id);
        footprint.setUserId(principal.getId());
        footprintMapper.insertSelective(footprint);
    }
}